package com.example.kaoqinmanager.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.example.kaoqinmanager.base.BaseBindingActivity;
import com.example.kaoqinmanager.bean.UserTable;
import com.example.kaoqinmanager.databinding.ActivityUpdatePasswordBinding;
import com.example.kaoqinmanager.utils.MyToast;

import org.litepal.LitePal;

import java.util.List;

public class UpdatePasswordActivity extends BaseBindingActivity<ActivityUpdatePasswordBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar.setTitle("修改密码");
        //显示返回箭头默认是不显示的
        actionBar.setDisplayHomeAsUpEnabled(true);

        getBinding().btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                update();
            }
        });

    }

    private void update() {
        String strAccount = getBinding().edAccount.getText().toString().trim();
        String strPassWord = getBinding().edPassWord.getText().toString().trim();
        String strPassWord2 = getBinding().edPassWord2.getText().toString().trim();
        if (TextUtils.isEmpty(strAccount)) {
            MyToast.showToast("旧密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(strPassWord)) {
            MyToast.showToast("新密码不能为空");
            return;
        }
        if (TextUtils.isEmpty(strPassWord2)) {
            MyToast.showToast("确认密码不能为空");
            return;
        }
        if (!strPassWord.equals(strPassWord2)) {
            MyToast.showToast("两次密码不一样");
            return;
        }

        List<UserTable> userTables = LitePal.where("userName = ?", mSharePreferenceUtil.getUserName()).find(UserTable.class);
        if (userTables.size() > 0) {
            UserTable userTable1 = userTables.get(0);

            if (userTable1.getPassWord().equals(strAccount)) {
                userTable1.setPassWord(strPassWord);
                userTable1.update(userTable1.getId());

                /*UserTable userTable = new UserTable();
                userTable.setPassWord(strPassWord);
                userTable.updateAll("userName = ?", mSharePreferenceUtil.getUserName());*/
                MyToast.showToast("修改成功");
                finish();
            } else {
                MyToast.showToast("旧密码不正确");
            }

        } else {
            MyToast.showToast("用户不存在");
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //actionbar的左侧图标的点击事件处理
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}